package com.ikkong.platform.controller;

import com.alibaba.druid.util.HttpClientUtils;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.system.utils.CommonUtil;
import com.jfinal.core.NotAction;
import com.jfinal.json.FastJson;
import com.sun.jndi.toolkit.url.Uri;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EncodingUtils;
import org.apache.http.util.EntityUtils;
import org.beetl.sql.core.kit.StringKit;

import java.beans.Encoder;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * copyright by 山东佳华百川传媒有限公司
 * 2016-10-08 15:59:45
 */

public class MobileApiController extends UrlPermissController {



    public void test(){
        Map<String, String[]> paraMap = getParaMap();
        String msg = getPara("msg", "");
        System.out.println(msg);
        renderText("111");
    }

//    public void testCode() {
//        String code = getPara("code");
//        if (StringKit.isBlank(code)) {
//            renderJson(error("未传入code"));
//            return;
//        }
//        String res = CommonUtil.codeToDetail(code);
//        renderJson(res);
//    }

    public void send(){
       String url = "http://127.0.0.1:8073/send";
        Map para = new HashMap();
        String str = URLEncoder.encode("我是机器人");
        para.put("msg",str);
        para.put("robot_wxid","wxid_en4wxv1xxnkt12");
        para.put("to_wxid","20671976800@chatroom");
        para.put("type",100);
        FastJson fj = new FastJson();
       String msg =  fj.toJson(para);

        String data1 = fj.toJson(Record.create().set("data", msg));

        String parse = "";

        try {

            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json");
            httpPost.setEntity(new StringEntity(data1));
            CloseableHttpResponse response = httpClient.execute(httpPost);
//		System.out.println(response.getStatusLine().getStatusCode() + "\n");
            HttpEntity entity = response.getEntity();
            parse = EntityUtils.toString(entity, "UTF-8");
//		System.out.println(responseContent);
            response.close();
            httpClient.close();










        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
       renderText(parse);
    }


}
